//
//  RegisterVC1.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/7/22.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "RegisterVC1.h"
#import "AppDelegate.h"
//#import "RegisterVC2.h"
@interface RegisterVC1 ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation RegisterVC1
{
    UIButton *_messCodeBtn;
    NSData* imageData;
    UIImageView *_iconImageV;
    UITextField *messCodeField;
    UITextField *_phoneField;
    UITextField *_PWField;
    UITextField *_againPWField;
    NSString *_typeStr;
    
    UIView *_view;
    UIButton *_btn;
    UILabel *_typeLb;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"快速注册";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];//title字体颜色
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    [self initView];
}
- (void)initView{
    _iconImageV = [[UIImageView alloc]init];
    _iconImageV.layer.cornerRadius = 5;
    _iconImageV .image = [UIImage imageNamed:@"logoo"];
    [self.view addSubview:_iconImageV];
    
    _phoneField = [[UITextField alloc]init];
    _phoneField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _phoneField.leftViewMode = UITextFieldViewModeAlways;
    _phoneField.layer.cornerRadius = 5;
    _phoneField.layer.borderWidth = 1;
    _phoneField.layer.borderColor = blackLineColore.CGColor;
    _phoneField.placeholder = @"请输入帐号(手机号码)";
    _phoneField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:_phoneField];

    messCodeField = [[UITextField alloc]init];
    messCodeField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    messCodeField.leftViewMode = UITextFieldViewModeAlways;
    messCodeField.layer.cornerRadius = 5;
    messCodeField.layer.borderWidth = 1;
    messCodeField.layer.borderColor = blackLineColore.CGColor;
    messCodeField.placeholder = @"请输入验证码";
    [self.view addSubview:messCodeField];
    
    _messCodeBtn = [[UIButton alloc]init];
    _messCodeBtn.backgroundColor = Theme_Color;
    _messCodeBtn.layer.cornerRadius = 5;
    _messCodeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_messCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_messCodeBtn addTarget:self action:@selector(messCodeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    _messCodeBtn.tintColor = [UIColor whiteColor];
    [self.view addSubview:_messCodeBtn];

    _PWField = [[UITextField alloc]init];
    _PWField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _PWField.leftViewMode = UITextFieldViewModeAlways;
    _PWField.layer.cornerRadius = 5;
    _PWField.layer.borderWidth = 1;
    _PWField.layer.borderColor = blackLineColore.CGColor;
    _PWField.placeholder = @"请设置您的密码";
    _PWField.secureTextEntry = YES;
    [self.view addSubview:_PWField];
    
    _againPWField = [[UITextField alloc]init];
    _againPWField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _againPWField.leftViewMode = UITextFieldViewModeAlways;
    _againPWField.layer.cornerRadius = 5;
    _againPWField.layer.borderWidth = 1;
    _againPWField.layer.borderColor = blackLineColore.CGColor;
    _againPWField.placeholder = @"请再次输入您的密码";
    _againPWField.secureTextEntry = YES;
    [self.view addSubview:_againPWField];
    
    UILabel *joinTeamLb = [[UILabel alloc]init];
    joinTeamLb.text = @"请选择注册类型";
    joinTeamLb.textColor = orangeTextColor;
//    joinTeamLb.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:joinTeamLb];
    
    UIView *bgView = [[UIView alloc]init];
    bgView.layer.borderWidth = 1;
    bgView.layer.borderColor = blackLineColore.CGColor;
    bgView.layer.cornerRadius = 3;
    [self.view addSubview:bgView];
    
    _typeLb = [[UILabel alloc]init];
    _typeLb.textAlignment = NSTextAlignmentCenter;
    _typeLb.textColor = Orange_Color;
//    _typeLb.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:_typeLb];
    
    _btn = [[UIButton alloc]init];
    [_btn setImage:[UIImage imageNamed:@"形状-2"] forState:UIControlStateNormal];
    [_btn setTitle:@"" forState:UIControlStateNormal];
    _btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_btn setTitleColor:Theme_Color forState:UIControlStateNormal];
    [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    _btn.imageEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
    [bgView addSubview:_btn];
    
    UIButton *doneBtn = [[UIButton alloc]init];
    doneBtn.backgroundColor = Theme_Color;
    doneBtn.layer.cornerRadius = 5;
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
    doneBtn.tintColor = [UIColor whiteColor];
    [self.view addSubview:doneBtn];
    
    //布局
    [_iconImageV mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(80),
        make.centerX.mas_equalTo(self.view),
        make.width.mas_equalTo(70),
        make.height.mas_equalTo(70);
    }];
    
    [_phoneField mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_iconImageV.mas_bottom).offset(20),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [messCodeField mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_phoneField.mas_bottom).offset(-1),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-110),
        make.height.mas_equalTo(40);
    }];
    
    [_messCodeBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_phoneField.mas_bottom).offset(-1),
        make.left.mas_equalTo(messCodeField.mas_right).offset(-1),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [_PWField mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(messCodeField.mas_bottom).offset(20),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [_againPWField mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_PWField.mas_bottom).offset(-1),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [joinTeamLb mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_againPWField.mas_bottom).offset(30),
        make.left.mas_equalTo(10),
        make.width.mas_equalTo(120),
        make.height.mas_equalTo(15);
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_againPWField.mas_bottom).offset(20),
        make.left.mas_equalTo(joinTeamLb.mas_right).offset(10),
        make.width.mas_equalTo(130),
        make.height.mas_equalTo(40);
    }];
    
    [_typeLb mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_againPWField.mas_bottom).offset(23),
        make.left.mas_equalTo(joinTeamLb.mas_right).offset(10),
        make.width.mas_equalTo(80),
        make.height.mas_equalTo(30);
    }];
    
    [_btn mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_againPWField.mas_bottom).offset(25),
        make.left.mas_equalTo(_typeLb.mas_right).offset(-10),
        make.width.mas_equalTo(60),
        make.height.mas_equalTo(30);
    }];

    [doneBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.bottom.mas_equalTo(self.view).offset(-20),
        make.left.mas_equalTo(52),
        make.right.mas_equalTo(-52),
        make.height.mas_equalTo(40);
    }];
}
//btn点击事件
- (void)messCodeBtnAction{
    if (![_phoneField.text isValidateMobile]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
    }
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_phoneField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            NSLog(@"获取验证码成功");
        }
        else
        {
            NSLog(@"错误信息：%@",error);
        }
    }];
    
    //更改button的颜色 并且设置button倒计时
    __block int timeout=30; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_messCodeBtn setBackgroundColor:Theme_Color];
                [_messCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                _messCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_messCodeBtn setBackgroundColor:LightGray_Color];
                [_messCodeBtn setTitle:[NSString stringWithFormat:@"%@秒后重试",strTime] forState:UIControlStateNormal];
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [UIView commitAnimations];
                _messCodeBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

- (void)doneAction{
//    if ([_phoneField.text isEqualToString:@""]) {
//        [SVProgressHUD showImage:nil status:@"请输入手机号"];
//    }
//    if ([messCodeField.text isEqualToString:@""]) {
//        [SVProgressHUD showImage:nil status:@"请输入验证码"];
//    }
//    if ([_PWField.text isEqualToString:@""]) {
//        [SVProgressHUD showImage:nil status:@"请输入密码"];
//    }
//    if ([_againPWField.text isEqualToString:@""]) {
//        [SVProgressHUD showImage:nil status:@"请再次输入密码"];
//    }
    if (_typeStr == nil) {
        [SVProgressHUD showImage:nil status:@"请选择注册类型"];
    }else{
        //注册
        //参数
        NSDictionary *parameters = @{
                                     @"phone":_phoneField.text,
                                     @"pass": _PWField.text,
                                     @"repass": _againPWField.text,
                                     @"type":_typeStr,
                                     @"sysType": @"ios",
                                     @"code":messCodeField.text,
                                     };
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer=[AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manager POST:@"http://115.28.152.130/tour/v1/user/register.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
            [SVProgressHUD showWithStatus:@"注册中"];
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            NSLog(@"%@>>>>",dic);
            NSString *error = dic[@"code"];
            if (![dic[@"code"] isEqualToString:@"0000"]) {
                [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            }else{
                //注册成功
                [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                
                [self backAction];
            }
            
        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"注册失败"];
        }];
    }
    
}
- (void)btnAction{
    _view = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth/2-65, 415, 130, 80)];
    _view.backgroundColor = White_Color;
    _view.layer.cornerRadius = 3;
    _view.layer.borderWidth = 1;
    _view.layer.borderColor = LightGray_Color.CGColor;
    [self.view addSubview:_view];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 40, 110, 1)];
    lineView.backgroundColor = LightGray_Color;
    [_view addSubview:lineView];
    UIButton *mesBoardBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 130, 40)];
    [mesBoardBtn setTitle:@"游客" forState:UIControlStateNormal];
    [mesBoardBtn addTarget:self action:@selector(tourseBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [mesBoardBtn setTitleColor:Black_Color forState:UIControlStateNormal];
    mesBoardBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_view addSubview:mesBoardBtn];
    UIButton *tipsBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 40, 130, 40)];
    [tipsBtn setTitle:@"俱乐部" forState:UIControlStateNormal];
    [tipsBtn addTarget:self action:@selector(clubBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [tipsBtn setTitleColor:Black_Color forState:UIControlStateNormal];
    tipsBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_view addSubview:tipsBtn];

    [_btn removeTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    [_btn addTarget:self action:@selector(hidenAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)tourseBtnAction{
    _view.hidden = YES;
    [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    _typeStr = @"2";
    _typeLb.text = @"游客";
}
- (void)clubBtnAction{
    _view.hidden = YES;
    [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    _typeStr = @"0";
    _typeLb.text = @"俱乐部";
}
- (void)hidenAction{
    _view.hidden = YES;
    [_btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
}
- (void)backAction{
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
